package com.ruoyi.tzai.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.tzai.mapper.AIModelConfigMapper;
import com.ruoyi.tzai.domain.AIModelConfig;
import com.ruoyi.tzai.service.IAIModelConfigService;
import com.ruoyi.common.utils.DateUtils;

/**
 * AI模型配置Service业务层处理
 * 
 * @author tzai
 * @date 2025-10-27
 */
@Service
public class AIModelConfigServiceImpl implements IAIModelConfigService 
{
    @Autowired
    private AIModelConfigMapper aiModelConfigMapper;

    /**
     * 查询AI模型配置
     * 
     * @param modelId AI模型配置主键
     * @return AI模型配置
     */
    @Override
    public AIModelConfig selectAIModelConfigByModelId(Long modelId)
    {
        return aiModelConfigMapper.selectAIModelConfigByModelId(modelId);
    }

    /**
     * 根据模型代码查询AI模型配置
     * 
     * @param modelCode 模型代码
     * @return AI模型配置
     */
    @Override
    public AIModelConfig selectAIModelConfigByModelCode(String modelCode)
    {
        return aiModelConfigMapper.selectAIModelConfigByModelCode(modelCode);
    }

    /**
     * 查询默认AI模型配置
     * 
     * @return AI模型配置
     */
    @Override
    public AIModelConfig selectDefaultAIModelConfig()
    {
        return aiModelConfigMapper.selectDefaultAIModelConfig();
    }

    /**
     * 查询AI模型配置列表
     * 
     * @param aiModelConfig AI模型配置
     * @return AI模型配置
     */
    @Override
    public List<AIModelConfig> selectAIModelConfigList(AIModelConfig aiModelConfig)
    {
        return aiModelConfigMapper.selectAIModelConfigList(aiModelConfig);
    }

    /**
     * 新增AI模型配置
     * 
     * @param aiModelConfig AI模型配置
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAIModelConfig(AIModelConfig aiModelConfig)
    {
        aiModelConfig.setCreateTime(DateUtils.getNowDate());
        // 默认状态：禁用
        if (aiModelConfig.getStatus() == null) {
            aiModelConfig.setStatus("1");
        }
        // 默认非默认模型
        if (aiModelConfig.getIsDefault() == null) {
            aiModelConfig.setIsDefault("0");
        }
        return aiModelConfigMapper.insertAIModelConfig(aiModelConfig);
    }

    /**
     * 修改AI模型配置
     * 
     * @param aiModelConfig AI模型配置
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAIModelConfig(AIModelConfig aiModelConfig)
    {
        aiModelConfig.setUpdateTime(DateUtils.getNowDate());
        return aiModelConfigMapper.updateAIModelConfig(aiModelConfig);
    }

    /**
     * 批量删除AI模型配置
     * 
     * @param modelIds 需要删除的AI模型配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAIModelConfigByModelIds(Long[] modelIds)
    {
        return aiModelConfigMapper.deleteAIModelConfigByModelIds(modelIds);
    }

    /**
     * 删除AI模型配置信息
     * 
     * @param modelId AI模型配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAIModelConfigByModelId(Long modelId)
    {
        return aiModelConfigMapper.deleteAIModelConfigByModelId(modelId);
    }

    /**
     * 设置为默认模型
     * 
     * @param modelId AI模型配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int setAsDefault(Long modelId)
    {
        // 先将所有模型设为非默认
        aiModelConfigMapper.clearAllDefaults();
        
        // 再设置当前模型为默认
        AIModelConfig config = new AIModelConfig();
        config.setModelId(modelId);
        config.setIsDefault("1");
        config.setUpdateTime(DateUtils.getNowDate());
        return aiModelConfigMapper.updateAIModelConfig(config);
    }

    /**
     * 测试连接
     * 
     * @param modelId AI模型配置主键
     * @return 结果
     */
    @Override
    public boolean testConnection(Long modelId)
    {
        // TODO: 实现AI模型连接测试
        AIModelConfig config = aiModelConfigMapper.selectAIModelConfigByModelId(modelId);
        if (config == null) {
            return false;
        }
        
        // 这里将来调用AI服务进行测试
        // return aiService.testConnection(config);
        
        return true;
    }
}

